import { Model, DataTypes, Optional } from 'sequelize';
import { sequelize } from '../database';

// 分类属性接口
export interface CategoryAttributes {
  id: string;
  name: string;
  slug: string;
  description: string | null;
  parent_id: string | null;
  order: number;
  icon: string | null;
  cover_image: string | null;
  article_count: number;
  is_active: boolean;
  meta_title: string | null;
  meta_description: string | null;
  created_at: Date;
  updated_at: Date;
  deleted_at: Date | null;
}

// 创建时可选的属性
interface CategoryCreationAttributes extends Optional<CategoryAttributes, 
  'id' | 'description' | 'parent_id' | 'order' | 'icon' | 'cover_image' | 
  'article_count' | 'is_active' | 'meta_title' | 'meta_description' | 
  'created_at' | 'updated_at' | 'deleted_at'> {}

// 分类模型类
class Category extends Model<CategoryAttributes, CategoryCreationAttributes> implements CategoryAttributes {
  public id!: string;
  public name!: string;
  public slug!: string;
  public description!: string | null;
  public parent_id!: string | null;
  public order!: number;
  public icon!: string | null;
  public cover_image!: string | null;
  public article_count!: number;
  public is_active!: boolean;
  public meta_title!: string | null;
  public meta_description!: string | null;
  
  // 时间戳
  public readonly created_at!: Date;
  public readonly updated_at!: Date;
  public readonly deleted_at!: Date | null;

  // 关联
  public readonly children?: Category[];
  public readonly parent?: Category;
}

// 初始化模型
Category.init(
  {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      validate: {
        notEmpty: true,
      },
    },
    slug: {
      type: DataTypes.STRING(100),
      allowNull: false,
      unique: true,
      validate: {
        notEmpty: true,
      },
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    parent_id: {
      type: DataTypes.UUID,
      allowNull: true,
      references: {
        model: 'categories',
        key: 'id',
      },
    },
    order: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    icon: {
      type: DataTypes.STRING(100),
      allowNull: true,
    },
    cover_image: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    article_count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
    },
    meta_title: {
      type: DataTypes.STRING(200),
      allowNull: true,
    },
    meta_description: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    deleted_at: {
      type: DataTypes.DATE,
      allowNull: true,
    },
  },
  {
    sequelize,
    modelName: 'category',
    tableName: 'categories',
    timestamps: true,
    paranoid: true, // 软删除
    underscored: true, // 使用下划线命名法
  }
);

// 自关联：父子分类关系
Category.hasMany(Category, { 
  as: 'children', 
  foreignKey: 'parent_id',
  onDelete: 'CASCADE',
});

Category.belongsTo(Category, { 
  as: 'parent', 
  foreignKey: 'parent_id',
});

export default Category; 